package com.swpu.cbg.cnshop.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.squareup.okhttp.Response;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.activity.CreateOrderActivity;
import com.swpu.cbg.cnshop.activity.MainActivity;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.adapter.CartAdapter;
import com.swpu.cbg.cnshop.bean.ShoppingCart;
import com.swpu.cbg.cnshop.bean.User;
import com.swpu.cbg.cnshop.http.OkHttpHelper;
import com.swpu.cbg.cnshop.http.SpotsCallback;
import com.swpu.cbg.cnshop.utils.CartProvider;
import com.swpu.cbg.cnshop.utils.JsonUtil;
import com.swpu.cbg.cnshop.utils.ToastUtil;
import com.swpu.cbg.cnshop.widget.cnToolbar;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenboge on 16/1/30.
 */
public class CartFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private LayoutInflater mInflater;
    private CartAdapter<ShoppingCart> mAdapter;
    private List<ShoppingCart> mDatas;
    private CartProvider mProvider;
    private List<ShoppingCart> carts;
    private float totleMoney;
    private static final int ACTION_EDIT = 0;
    private static final int ACTION_ACHIEVE = 1;
    private cnToolbar mToolBar;
    private OkHttpHelper mHelper = OkHttpHelper.getInstance();


    @ViewInject(R.id.id_cart_recyclerview)
    private RecyclerView mRecyclerView;

    @ViewInject(R.id.id_cart_checkbox)
    private CheckBox mCheckBox;

    @ViewInject(R.id.id_cart_totle)
    private TextView mTotleText;

    @ViewInject(R.id.id_cart_resume)
    private Button mResumBtn;

    @ViewInject(R.id.id_cart_delete)
    private Button mDeleteBtn;


    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mInflater = LayoutInflater.from(getActivity());
        mView = mInflater.inflate(R.layout.cart, container, false);
        return mView;
    }

    @Override
    public void init() {
        mProvider = new CartProvider(getActivity());
        initData();
        initView();
    }

    private void initView() {
        mResumBtn.setOnClickListener(this);
        mDeleteBtn.setOnClickListener(this);
    }

    private void initData() {
        mDatas = mProvider.getAll();
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mAdapter = new CartAdapter<ShoppingCart>(getActivity(), mDatas, R.layout.cart_view_item, mCheckBox, mTotleText);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    }


    public void refreshData() {
        mAdapter.ClearData();
        mDatas = mProvider.getAll();
        mAdapter.AddData(mDatas);
        mAdapter.showTotleText();
    }

//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        if (context != null) {
//            if (context instanceof MainActivity) {
//                MainActivity mainActivity = (MainActivity) context;
//                mToolBar = (cnToolbar) mainActivity.findViewById(R.id.toolbar);
//                mToolBar.getmEditButton().setTag(ACTION_EDIT);
//                mToolBar.hideRightButton();
//                mToolBar.hideContent();
//                mToolBar.setEditButtonText(R.string.edit);
//                mToolBar.getmEditButton().setOnClickListener(this);
//            }
//
//        }
//    }

    private void changeToolBar() {
        mToolBar.setEditButtonText(R.string.edit);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_cart_delete:
                mAdapter.delCart();
                Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                break;

            case R.id.id_cart_resume:
                Intent intent = new Intent();
                intent.setClass(getActivity(), CreateOrderActivity.class);
                startActivity(intent, true);
                break;

            default:
                switch (Integer.parseInt(v.getTag().toString())) {
                    case ACTION_EDIT:
                        showEditState();
                        break;

                    case ACTION_ACHIEVE:
                        showNormalState();
                        break;
                }
                break;
        }

    }


    private void showNormalState() {
        mToolBar.hideRightButton();
        mToolBar.getmEditButton().setTag(ACTION_EDIT);
        mToolBar.setEditButtonText(R.string.edit);
        mTotleText.setVisibility(View.VISIBLE);
        mDeleteBtn.setVisibility(View.GONE);
        mResumBtn.setVisibility(View.VISIBLE);
        mAdapter.checkAll_None(true);
        mCheckBox.setChecked(true);
        mAdapter.showTotleText();
    }

    private void showEditState() {
        mToolBar.hideRightButton();
        mToolBar.setEditButtonText(R.string.achieve);
        mToolBar.getmEditButton().setTag(ACTION_ACHIEVE);
        mTotleText.setVisibility(View.GONE);
        mDeleteBtn.setVisibility(View.VISIBLE);
        mResumBtn.setVisibility(View.GONE);
        mAdapter.checkAll_None(false);
        mCheckBox.setChecked(false);
    }


}
